package com.pkk.penguin.frame.know.java.Javacoreknowledge.thread.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author peikunkun
 * @version V1.0
 * @date 2020-08-21 14:59
 * @since 公共线程池的使用
 **/
public class ComonThreadPool {


  /**
   * 域名检测池核心线程
   */
  private static final Integer CHECK_DOMAIN_THREAD_CORE = 15;
  /**
   * 最大线程数量
   */
  private static final Integer CHECK_DOMAIN_THREAD_MAX = 50;
  /**
   * 空闲线程(#CHECK_DOMAIN_THREAD_MAX-#CHECK_DOMAIN_THREAD_CORE)的空闲线程保留时间
   */
  private static final Integer CHECK_DOMAIN_THREAD_KEEP_ALIVE = 60;

  /**
   * 最大允许队列等待的任务数
   */
  private static final Integer CHECK_DOMAIN_THREAD_MAX_TASK = 100 * 1000;
  private static final BlockingQueue<Runnable> CHECK_DOMAIN_THREAD_QUEUE = new ArrayBlockingQueue<Runnable>(
      CHECK_DOMAIN_THREAD_MAX_TASK, Boolean.TRUE);

  /**
   * 检测的公共线程
   */
  public static ThreadPoolExecutor CHECK_DOMAIN_THREAD = new ThreadPoolExecutor(CHECK_DOMAIN_THREAD_CORE,
      CHECK_DOMAIN_THREAD_MAX, CHECK_DOMAIN_THREAD_KEEP_ALIVE, TimeUnit.SECONDS, CHECK_DOMAIN_THREAD_QUEUE,
      new ThreadFactory() {
        /**
         * Constructs a new {@code Thread}.  Implementations may also initialize priority, name, daemon status,
         * {@code ThreadGroup}, etc.
         *
         * @param r a runnable to be executed by new thread instance
         * @return constructed thread, or {@code null} if the request to create a thread is rejected
         */
        @Override
        public Thread newThread(Runnable r) {
          return new Thread(r, "ZhiYi-CheckDomain-" + r.hashCode());
        }
        //如果线程还是不够用,那就谁发起的谁执行吧（交给调用者线程执行(主线程)）
      }, new ThreadPoolExecutor.CallerRunsPolicy());

}
